package com.yoo.ons.function.like.adapter

import android.os.Build
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.yoo.ons.R
import com.yoo.ons.base.adpater.BasePagedAdapter
import com.yoo.ons.base.adpater.ItemType.ErrorPlaceHolder
import com.yoo.ons.base.adpater.ItemType.NoMorePlaceHolder
import com.yoo.ons.base.adpater.NoMoreVH
import com.yoo.ons.function.like.viewmodel.ItemType
import com.yoo.ons.model.WlmSubListModel
import com.yoo.ons.util.inflate

class LikeDataListAdapter : BasePagedAdapter() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemType.LikeListData -> LikeDataListViewHolder(parent, callback)
            com.yoo.ons.base.adpater.ItemType.EmptyPlaceHolder -> LikeDataListEmptyViewHolder(
                parent,
                callback
            )

            NoMorePlaceHolder -> NoMoreVH(parent.inflate(R.layout.layout_wlm_nomore))
            ErrorPlaceHolder -> LikeDataListErrorViewHolder(parent, callback)
            else -> super.onCreateViewHolder(parent, viewType)
        }
    }

    @RequiresApi(Build.VERSION_CODES.S)
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = getItem(position)
        if (itemCount == 1 && holder is LikeDataListEmptyViewHolder) {
            // 如果只有一个 item，调整布局参数使其占满整个宽度
            val layoutParams =
                holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
            layoutParams.isFullSpan = true
        } else if (itemCount == 1 && holder is LikeDataListErrorViewHolder) {
            val layoutParams =
                holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
            layoutParams.isFullSpan = true
        } else {
            when (holder) {
                is LikeDataListViewHolder -> item?.data<WlmSubListModel>()
                    ?.let { holder.bind(it, position) }

                is NoMoreVH -> {
                    val layoutParams =
                        holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
                    layoutParams.isFullSpan = true
                }
            }
        }

    }
}